package com.yuke.cloud.service.pmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.ProdIdsDto;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.pmc.dto.ProdSpecByCategoryDto;
import com.yuke.cloud.service.pmc.dto.ProdSpecInfoAndPriceDto;
import com.yuke.cloud.service.pmc.entity.ProdSpec;
import com.yuke.cloud.service.pmc.service.ProdSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Created by zys on 2018/09/30.
*/
@Slf4j
@Api(value = "WEB - ProdSpecController", tags = "处理ProdSpec相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/spec")
public class ProdSpecController extends BaseController {
    @Resource
    private ProdSpecService prodSpecService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "prodSpec", value = "该数据记录的POJO") @RequestBody ProdSpec prodSpec) {
        prodSpecService.save(prodSpec);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        prodSpecService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodSpec", value = "需要修改的内容") @RequestBody ProdSpec prodSpec) {
        prodSpecService.update(prodSpec);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ProdSpec> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ProdSpec prodSpec = prodSpecService.findById(id);
        return WrapMapper.ok(prodSpec);
    }

    @PostMapping("/specName")
    @ApiOperation(httpMethod = "POST", value = "根据商品规格名和categoryId获取spec_id和spec_name信息")
    public Wrapper<List<ProdSpecInfoDto>> getBySpecName(@ApiParam(name = "id", value = "记录id") @RequestBody ProdSpecInfoDto prodSpecInfoDto) {
        return WrapMapper.ok(prodSpecService.getBySpecName(prodSpecInfoDto));
    }

    @PostMapping("/getSpecBySpecName")
    public Wrapper<List<ProdSpecInfoDto>> getSpecBySpecName(@RequestBody ProdSpecInfoDto prodSpecInfoDto) {
        return WrapMapper.ok(prodSpecService.getSpecBySpecName(prodSpecInfoDto));
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdSpec>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdSpec> list = prodSpecService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /*@GetMapping("/app/recommend")
    @ApiOperation(httpMethod = "GET", value = "app首页商品推荐")
    public Wrapper<List<ProdSpecInfoAndPriceDto>> recommendList(@RequestParam("longitude") String longitude, @RequestParam("latitude") String latitude) {
        List<ProdSpecInfoAndPriceDto> prodSpecInfoAndPriceDtos = prodSpecService.getListRecommend(longitude, latitude);
        return WrapMapper.ok(prodSpecInfoAndPriceDtos);
    }*/

    @PostMapping("/app/like")
    @ApiOperation(httpMethod = "POST", value = "app猜你喜欢，价格为全国最低价")
    public Wrapper<List<ProdSpecInfoAndPriceDto>> getSpecInfoDtosBySpecIds(@RequestBody ProdIdsDto prodIdsDto) {
        List<Long> specIds = new ArrayList<>();
        if (prodIdsDto.getIds() != null && !prodIdsDto.getIds().equals("")) {
            specIds = Stream.of(prodIdsDto.getIds().split(",")).map(obj -> Long.valueOf(obj)).collect(Collectors.toList());
        }
        List<ProdSpecInfoAndPriceDto> prodSpecInfoDtos= prodSpecService.getSpecInfoDtosBySpecIds(specIds);
        return WrapMapper.ok(prodSpecInfoDtos);
    }

    @PostMapping("/app/specIds")
    public Wrapper<List<ProdSpecInfoDto>> getSpecInfoBySpecId(@RequestBody List<Long> specIds) {
        return WrapMapper.ok(prodSpecService.getSpecInfoBySpecIds(specIds));
    }

    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "根据specName判断该商品是否存在")
    public Wrapper ifExist(@ApiParam(name = "id", value = "记录id") @RequestBody ProdSpec prodSpec) {
        Example example = new Example(ProdSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("specName", prodSpec.getSpecName());

        if (prodSpec.getSpecId() != null) {
            criteria.andNotEqualTo("specId", prodSpec.getSpecId());
        }
        int count = prodSpecService.selectCountByExample(example);
        return WrapMapper.ok(count);
    }


    //商品禁用时，商家库存的该商品也强制禁用，商家无法启用（要返回给商家spec.is_enable）

    @GetMapping("/app/purchase/{shopId}/{categoryId}")
    @ApiOperation(httpMethod = "GET", value = "店铺进货页面，根据categoryId获取商品信息")
    public Wrapper<List<ProdSpecByCategoryDto>> getListByCategoryId(@ApiParam(name = "shopId", value = "店铺shopId") @PathVariable(value = "shopId") Long shopId,
                                                                    @ApiParam(name = "categoryId", value = "商品类别Id") @PathVariable(value = "categoryId") Long categoryId) {
        List<ProdSpecByCategoryDto> list = prodSpecService.getSpecByCategoryId(categoryId, shopId);
        return WrapMapper.ok(list);
    }


    @PutMapping("/update")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper updateSpecCode(@ApiParam(name = "prodSpec", value = "需要修改的内容") @RequestBody ProdSpec prodSpec) {
        prodSpecService.updateSpecCode(prodSpec);
        return WrapMapper.ok();
    }

    @PostMapping("/getSpecInfoBySpecName")
    public Wrapper<PageInfo<ProdSpecInfoDto>> getSpecBySpecName(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                                @RequestBody ProdSpecInfoDto prodSpecInfoDto) {
        PageHelper.startPage(page, size);
        List<ProdSpecInfoDto> prodSpecInfoDtos = prodSpecService.getSpecBySpecName(prodSpecInfoDto);
        PageInfo pageInfo = new PageInfo(prodSpecInfoDtos);
        return WrapMapper.ok(pageInfo);
    }
}
